package com.crack.nga2.volley;


import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.crack.nga2.utils.HttpParams;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * 登录请求
 * Created by Administrator on 2016/1/6.
 */
public class LoginRequest extends Request<String> {
    static String url = "http://bbs.ngacn.cc/nuke.php?func=login";
    String type_1 = "use_name";
    String type_2 = "use_mail";
    private Response.Listener<String> listener;
    private StringBuilder cookies = new StringBuilder();
    private String username;
    private String password;

    public LoginRequest(String username, String password, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(Method.POST, url, errorListener);
        this.listener = listener;
        this.username = username;
        this.password = password;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        try {
            login178(username, password);
            return Response.success(cookies.toString(), HttpHeaderParser.parseCacheHeaders(response));
        } catch (IOException | NullPointerException e) {
            e.printStackTrace();
            return Response.error(new VolleyError(e.getMessage()));
        }
    }

    @Override
    protected void deliverResponse(String response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }

    private void login178(String username, String password) throws IOException {
        String loginType;
        if (username.contains("@")) {
            loginType = type_2;
        } else {
            loginType = type_1;
        }
        HttpParams params = new HttpParams();
        params.put("login_type", loginType);
        params.put("username", username);
        params.put("password", password);
        //第一步登陆 获取178的Cookie
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.connect();
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "GBK");
        writer.write(params.toString());
        writer.flush();
        writer.close();
        //获取Cookie
        Map<String, List<String>> headerFields = conn.getHeaderFields();
        List<String> list = headerFields.get("Set-Cookie");
        for (String string : list) {
            //只取第一部分
            cookies.append(string.substring(0, string.indexOf(";") + 1));
        }
        //第二部 登陆NGA
        loginNga();
    }

    private void loginNga() throws IOException {
        String url = "http://bbs.ngacn.cc/";
        HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
        conn.addRequestProperty("Cookie", cookies.toString());
        conn.connect();
        List<String> list = conn.getHeaderFields().get("Set-Cookie");
        for (String string : list) {
            //只取第一部分
            cookies.append(string.substring(0, string.indexOf(";") + 1));
        }
        //Cookie获取完成

    }
}
